//
//  PagesViewController.m
//  LineByLine
//
//  Created by 병철 김 on 11. 9. 28..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PagesViewController.h"
#import "PageCell.h"
#import "ReadingViewController.h"
#import "CoreDataManager.h"
#import "BookStoreBookViewController.h"
#import "WritingViewController.h"
#import "BookStoreBooksController.h"
#import "BooksController.h"


@implementation PagesViewController

- (id)init {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        cellMap = [[NSMutableDictionary alloc] initWithCapacity:5];
        cellTitle = [[NSMutableDictionary alloc] initWithCapacity:5];
        
        // section title 
        [cellTitle setObject:@"첫 화면" forKey:[NSNumber numberWithInt:0]];
        [cellTitle setObject:@"Books" forKey:[NSNumber numberWithInt:1]];
        [cellTitle setObject:@"Book Store" forKey:[NSNumber numberWithInt:2]];
        
        // section 0 cells
        PageCell *cellMainPage = [[PageCell alloc] initWithTitle:@"Main Page" controller:nil];
        PageCell *cellBooksView = [[PageCell alloc] initWithTitle:@"Books" controller:[BooksController class]];
        NSArray *section0 = [[NSArray alloc] initWithObjects:cellMainPage, cellBooksView, nil];
        [cellMap setObject:section0 forKey:[NSNumber numberWithInt:0]];
        [section0 release];
        [cellMainPage release];
        
        // section 1 cells
        PageCell *cellReadView = [[PageCell alloc] initWithTitle:@"Step1 Reading" controller:[ReadingViewController class]];
        PageCell *cellListenView = [[PageCell alloc] initWithTitle:@"Step2 Listening" controller:[ReadingViewController class]];
        PageCell *cellSpeakingView = [[PageCell alloc] initWithTitle:@"Step3 Speaking" controller:[ReadingViewController class]];
        PageCell *cellWritingView = [[PageCell alloc] initWithTitle:@"Step4 Writing" controller:[WritingViewController class]];
        PageCell *cellListening2View = [[PageCell alloc] initWithTitle:@"Step2-2 Listening All" controller:[ReadingViewController class]];
        PageCell *cellSpeaking2View = [[PageCell alloc] initWithTitle:@"Step3-2 Speaking All" controller:[ReadingViewController class]];
        
        NSArray *section1 = [[NSArray alloc] initWithObjects:cellReadView, cellListenView, cellSpeakingView, cellWritingView, cellListening2View, cellSpeaking2View, nil];
        [cellMap setObject:section1 forKey:[NSNumber numberWithInt:1]];
        [section1 release];
        [cellReadView release];
        [cellListenView release];
        [cellSpeakingView release];
        [cellWritingView release];
        [cellListening2View release];
        [cellSpeaking2View release];
        
        // section 2 cells
        PageCell *bookStoreBooks = [[PageCell alloc] initWithTitle:@"Book Store Books" controller:[BookStoreBooksController class]];
        PageCell *bookStoreView = [[PageCell alloc] initWithTitle:@"Book Store Book Detail" controller:[BookStoreBookViewController class]];
        NSArray *section2 = [[NSArray alloc] initWithObjects:bookStoreBooks, bookStoreView, nil];
        [cellMap setObject:section2 forKey:[NSNumber numberWithInt:2]];
        [section2 release];
        [bookStoreBooks release];
        [bookStoreView release];
    }
    return self;
}

- (void)dealloc {
    [cellMap release];
    [cellTitle release];
    [super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [cellMap count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *cells = [cellMap objectForKey:[NSNumber numberWithInt:section]];
    return [cells count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *cells = [cellMap objectForKey:[NSNumber numberWithInt:[indexPath section]]];
    return [cells objectAtIndex:[indexPath row]];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [cellTitle objectForKey:[NSNumber numberWithInt:section]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PageCell *cell = (PageCell *) [tableView cellForRowAtIndexPath:indexPath];
    Class controllerClass = [cell controller];
    
    UIViewController *controller = [[controllerClass alloc] init];
    if ([indexPath section] == 1) {
        CoreDataManager *coreDataManager = [CoreDataManager sharedCoreDataManager];
        id book = [coreDataManager selectOneFrom:@"Book" where:@"bookId=%@", [NSNumber numberWithInt:745]];
        [controller setValue:book forKey:@"book"];
        [controller setValue:[NSNumber numberWithInt:[indexPath row]+1] forKey:@"step"];
    }
    if ([indexPath section] == 2 && [indexPath row] == 1) {
        [controller setValue:[NSNumber numberWithInt:745] forKey:@"bookId"];
    }
    
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}
@end
